﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class FixedTaxRuleTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("name", 10)]
        public void Can_CRUD(string name, decimal amount)
        {
            var rule = Processor.Execute(new CreateFixedTaxRule
            {
                Name = name,
                CommonFlags = SalesRuleFlags.None,
                Flags = FixedTaxRuleFlags.Value,
                Amount = amount
            }).Result;

            Assert.NotNull(rule);
            rule.Name.AreEqualIgnoringCase(name);
            rule.Amount.AreEqual(amount);
            rule.AreEqual(Processor.Execute(new GetSalesRule
            {
                EntityID = rule.EntityID
            }).Result);

            name = name.Reverse();
            amount = amount * 2;
            var upd_rule = Processor.Execute(new UpdateFixedTaxRule
            {
                EntityID = rule.EntityID,
                Name = name,
                CommonFlags = SalesRuleFlags.Active,
                Flags = FixedTaxRuleFlags.Percent,
                Amount = amount
            }).Result;

            Assert.NotNull(upd_rule);
            upd_rule.AreEqual(rule);
            upd_rule.Name.AreEqualIgnoringCase(name);
            upd_rule.Amount.AreEqual(amount);
            upd_rule.CommonFlags.AreEqual(SalesRuleFlags.Active);
            upd_rule.Flags.AreEqual(FixedTaxRuleFlags.Percent);

            Processor.Execute(new DeleteSalesRule
            {
                EntityID = rule.EntityID
            });
        }

        [TestCase(0)]
        [TestCase(-1)]
        public void Cannot_CreateUpdate_If_Amount_LtOrEq_Zero(decimal amount)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateFixedTaxRule
            {
                Amount = amount
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateFixedTaxRule
            {
                Amount = amount
            }));
        }
    }
}
